package com.ruoyi.unit.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.unit.domain.StockUnit;
import com.ruoyi.unit.service.IStockUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存单位管理Controller
 * 
 * @author yx
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/unit/unit")
public class StockUnitController extends BaseController
{
    @Autowired
    private IStockUnitService stockUnitService;

    /**
     * 查询库存单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockUnit stockUnit)
    {
        startPage();
        List<StockUnit> list = stockUnitService.selectStockUnitList(stockUnit);
        return getDataTable(list);
    }

    /**
     * 导出库存单位管理列表
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:export')")
    @Log(title = "库存单位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockUnit stockUnit)
    {
        List<StockUnit> list = stockUnitService.selectStockUnitList(stockUnit);
        ExcelUtil<StockUnit> util = new ExcelUtil<StockUnit>(StockUnit.class);
        util.exportExcel(response, list, "库存单位管理数据");
    }

    /**
     * 获取库存单位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Integer unitId)
    {
        return success(stockUnitService.selectStockUnitByUnitId(unitId));
    }

    /**
     * 新增库存单位管理
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:add')")
    @Log(title = "库存单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockUnit stockUnit)
    {
        return toAjax(stockUnitService.insertStockUnit(stockUnit));
    }

    /**
     * 修改库存单位管理
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:edit')")
    @Log(title = "库存单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockUnit stockUnit)
    {
        return toAjax(stockUnitService.updateStockUnit(stockUnit));
    }

    /**
     * 删除库存单位管理
     */
    @PreAuthorize("@ss.hasPermi('unit:unit:remove')")
    @Log(title = "库存单位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Integer[] unitIds)
    {
        return toAjax(stockUnitService.deleteStockUnitByUnitIds(unitIds));
    }
}
